package com.neusoft.bookstore.menu.controller;

import com.neusoft.bookstore.customer.model.Customer;
import com.neusoft.bookstore.menu.model.Menu;
import com.neusoft.bookstore.menu.service.MenuService;
import com.neusoft.bookstore.util.ErrorCode;
import com.neusoft.bookstore.util.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName MenuController
 * @Author yins
 * @Date 2020/4/27 11:20
 */

@Api("menu")
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;



    /**
     * 新增菜单
     * @param menu 封装了所有前端页面的菜单信息
     * @return
     */
    @ApiOperation(value = "新增菜单",notes = "新增菜单")
    @PostMapping("addMenu")
    public ResponseVo addMenu(Menu menu){
        ResponseVo responseVo = new ResponseVo();
        try {
            responseVo=menuService.addMenu(menu);
        }catch (Exception e){
            //处理异常 提示前台 服务端 有异常
            responseVo.setCode(ErrorCode.SERVER_EXCEPTION_CODE);
            responseVo.setSuccess(false);
            responseVo.setMsg("系统异常");
            // 服务端后台 打印异常
            e.printStackTrace();
        }
        return responseVo;
    }

    /**
     * 菜单树查询
     * @return
     */
    @ApiOperation(value = "菜单树查询",notes = "菜单树查询")
    @GetMapping("listMenuTree")
    public ResponseVo listMenuTree(){
        ResponseVo responseVo = new ResponseVo();
        try {
            responseVo=menuService.listMenuTree();
        }catch (Exception e){
            //处理异常 提示前台 服务端 有异常
            responseVo.setCode(ErrorCode.SERVER_EXCEPTION_CODE);
            responseVo.setSuccess(false);
            responseVo.setMsg("系统异常");
            // 服务端后台 打印异常
            e.printStackTrace();
        }
        return responseVo;
    }


    /**
     * 菜单详情查询
     * @return
     */
    @ApiOperation(value = "菜单详情查询",notes = "菜单详情查询")
    @GetMapping("findMenuByCode")
    public ResponseVo findMenuByCode(String menuCode){
        ResponseVo responseVo = new ResponseVo();
        try {
            responseVo=menuService.findMenuByCode(menuCode);
        }catch (Exception e){
            //处理异常 提示前台 服务端 有异常
            responseVo.setCode(ErrorCode.SERVER_EXCEPTION_CODE);
            responseVo.setSuccess(false);
            responseVo.setMsg("系统异常");
            // 服务端后台 打印异常
            e.printStackTrace();
        }
        return responseVo;
    }



    /**
     * 修改菜单信息
     * @return
     */
    @ApiOperation(value = "修改菜单信息",notes = "修改菜单信息")
    @PostMapping("updateMenuByCode")
    public ResponseVo updateMenuByCode(Menu menu){
        ResponseVo responseVo = new ResponseVo();
        try {
            responseVo=menuService.updateMenuByCode(menu);
        }catch (Exception e){
            //处理异常 提示前台 服务端 有异常
            responseVo.setCode(ErrorCode.SERVER_EXCEPTION_CODE);
            responseVo.setSuccess(false);
            responseVo.setMsg("系统异常");
            // 服务端后台 打印异常
            e.printStackTrace();
        }
        return responseVo;
    }


    /**
     * 删除菜单信息
     * @return
     */
    @ApiOperation(value = "删除菜单信息",notes = "删除菜单信息")
    @GetMapping("deleteMenuByCode")
    public ResponseVo deleteMenuByCode(String menuCode){
        ResponseVo responseVo = new ResponseVo();
        try {
            responseVo=menuService.deleteMenuByCode(menuCode);
        }catch (Exception e){
            //处理异常 提示前台 服务端 有异常
            responseVo.setCode(ErrorCode.SERVER_EXCEPTION_CODE);
            responseVo.setSuccess(false);
            responseVo.setMsg("系统异常");
            // 服务端后台 打印异常
            e.printStackTrace();
        }
        return responseVo;
    }

}
